<?php

namespace App\Models;

use AliyunMNS\Traits\MessageIdAndMD5;
use Code;

class Login extends BaseModel
{
    protected $table = 'member';

    protected $primaryKey = 'id';

    public $timestamps = false;

    protected $guarded = [];

    public function getOne($where, $fileds = '*', $order = '')
    {
        if (!$order) {
            $order = ['id' => 'desc'];
        }
        return $this->multiSelect($fileds)->multiWhere($where)->multiOrder($order)->first();
    }

    /**
     * 按条件查询全部数据,根据配置显示条数显示
     */
    public function getList(array $where = [], $fields = '*', $order = '', $pageSize = '')
    {
        if ($pageSize) {
            return $this->multiSelect($fields)->multiWhere($where)->multiOrder($order)->paginate($pageSize);
        } else {
            return $this->multiSelect($fields)->multiWhere($where)->multiOrder($order)->get();
        }
    }

    /**
     *插入数据
     */
    public function add($data)
    {
        return $this->_add($data);
    }


    /***
     * @param array $id
     * @param array $data
     * 更新数据
     */
    public function up($where, $data)
    {
        return $this->_updata($where, $data);
    }

    /**
     * @param $id
     * 删除数据
     */
    public function del($where)
    {
        return $this->_del($where);
    }



    public function login($request)
    {
        //搜集用户登录信息
        $member_mobile = $request['member_mobile'];
        $member_passwd = md5($request->input('member_passwd'));
        $data['ip'] = getIp();
        $code1 = $request->input('code');
        if (!check_captcha($code1)) {
            $res['state'] = 0;
            $res['msg'] = '验证码错误';
            return $res;
        }
        $where['member_mobile'] = $member_mobile;
        $where['member_passwd'] = $member_passwd;
        $all = $this->getOne($where);
        $data['lastip'] = $all['ip'];
        if ($all) {
            $user['name'] = $member_mobile;
            $user['ip'] = $all['ip'];
            session(['user' => $user]);
            session(['uid' => $all['id']]);
            //更新ip地址入库
            $this->up($where, $data);
            $res['state'] = 200;
            $res['msg'] = '登陆成功';
            return $res;
        }
        $res['state'] = 400;
        $res['msg'] = '用户名或密码错误！';
        return $res;
    }


}
